package com.songlanyun.modules.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.songlanyun.common.utils.PageUtils;
import com.songlanyun.common.utils.R;
import com.songlanyun.modules.goods.dao.GoodsDao;
import com.songlanyun.modules.goods.entity.Goods;
import com.songlanyun.modules.goods.service.GoodsService;
import com.songlanyun.modules.information.entity.InformationEntity;
import com.songlanyun.modules.information.service.InformationService;
import com.songlanyun.modules.popup.dao.PopUpDao;
import com.songlanyun.modules.popup.entity.PopUp;
import com.songlanyun.modules.user.controller.AbstractController;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("api/popconfig")
public class PopUpController extends AbstractController {

    @Autowired
    private PopUpDao popUpDao;
    @Autowired
    private GoodsService goodsService;
    @Autowired
    private InformationService informationService;
    /**
     * 信息
     */
    @GetMapping("/info")
    //@RequiresPermissions("pointconfig:pointconfig:info")
    public R load(){
        PopUp popUp = popUpDao.selectOne(new LambdaQueryWrapper<PopUp>().orderByDesc(PopUp::getId).last("LIMIT 1"));
        return R.ok().put("data", popUp);
    }

    //管理平台，首页弹窗接口
    @GetMapping("/info/info")
    public R info(){
        PopUp popUp = popUpDao.selectOne(new LambdaQueryWrapper<PopUp>().orderByDesc(PopUp::getId).last("LIMIT 1"));
        Goods goods = goodsService.getById(popUp.getProductId());
        if (goods != null && goods.getSpuCode() != null){
            popUp.setSpuCode(goods.getSpuCode());
        }
        List<InformationEntity> list = informationService.getListTitleAndId();
        return R.ok().put("data", popUp).put("information", list);
    }
    /**
     * 保存信息
     */
    @PostMapping("/set")
    //@RequiresPermissions("pointconfig:pointconfig:info")
    public R set(@RequestBody  HashMap<String,Object> map){

        System.out.println(map);

        Integer type = Integer.valueOf(map.get("type").toString());
        PopUp popUp = new PopUp();
        popUp.setId(Long.valueOf(map.get("id").toString()));
        popUp.setType(Integer.valueOf(map.get("type").toString()));
        popUp.setImage(map.get("image").toString());
        if (map.get("logo").toString()!= null){
            popUp.setImage(map.get("logo").toString());
        }
        if (type == 1){
            Goods goods = goodsService.getOne(new LambdaQueryWrapper<Goods>()
                    .eq(Goods::getSpuCode, map.get("spu_code").toString())
                    .last("LIMIT 1")
            );
            popUp.setProductId(goods.getId());
        }
        if (type == 2){
            popUp.setProductId(Long.valueOf(map.get("informationId").toString()));
        }
        popUpDao.updateById(popUp);
        return R.ok();
    }

}
